package com.liveinstars.im.core.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.liveinstars.api.dto.BatchImGetUserReq;
import com.liveinstars.api.dto.ImGetUserInfo;
import com.liveinstars.api.facade.ImUserCentreApi;
import com.liveinstars.im.domain.dao.model.ChatGroup;
import com.liveinstars.im.domain.dao.model.ChatGroupMember;
import com.liveinstars.im.domain.dao.model.Friends;
import com.liveinstars.im.domain.manager.ChatGroupManager;
import com.liveinstars.im.domain.manager.ChatGroupMemberManager;
import com.liveinstars.im.domain.manager.FriendsManager;
import com.liveinstars.api.im.query.FriendsQuery;
import com.liveinstars.api.im.vo.UserVO;
import com.liveinstars.im.core.service.FriendsService;
import com.onepiece.shipelves.common.Result;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: zhouze
 * @date: 2020/4/27
 */
@Service("friendsService")
public class FriendsServiceImpl implements FriendsService {

    @Autowired
    private FriendsManager friendsManager;

    @Autowired
    private ChatGroupManager chatGroupManager;

    @Autowired
    private ChatGroupMemberManager chatGroupMemberManager;

    @Autowired
    private ImUserCentreApi imUserCentreApi;

    @Override
    public List<Friends> listByUserIdAndType(Long userId, Integer type) {
        List<Friends> friendsList = friendsManager.listFriendsByUserIdAndType(userId, type);
        return friendsList;
    }

    @Override
    public List<UserVO> listFriendsByUserIdAndType(Long userId, Integer type) {
        List<UserVO> list = Lists.newArrayList();
        List<Friends> friendsList = this.listByUserIdAndType(userId, type);
        List<Long> userIdList = friendsList.stream().map(Friends::getFriendId).collect(Collectors.toList());
        BatchImGetUserReq req = new BatchImGetUserReq();
        req.setUids(userIdList);
        Result<List<ImGetUserInfo>> listResult = imUserCentreApi.batchImGetUserInfo(req);
        if (listResult.getCode().equals(Result.SUCCESS)) {
            HashMap<Long, ImGetUserInfo> userMap = Maps.newHashMap();
            listResult.getData().forEach(user -> {
                userMap.put(user.getUid(), user);
            });
            friendsList.forEach(friends -> {
                if (userMap.containsKey(friends.getFriendId())) {
                    ImGetUserInfo userInfo = userMap.get(friends.getFriendId());
                    UserVO userVO = new UserVO();
                    userVO.setLogo(userInfo.getLogo());
                    userVO.setGroupId(userInfo.getGroupId());
                    userVO.setDate(userInfo.getRegdate().toString());
                    userVO.setIndividualResume(userInfo.getIndividualResume());
                    userVO.setNickName(userInfo.getNickName());
                    userVO.setName(userInfo.getUserName());
                    userVO.setId(userInfo.getUid());
                    userVO.setGroupName(ChatGroup.UserLevel.getUserLevel(userInfo.getGroupId()).getValue());
                    userVO.setRemarkName(friends.getRemarkName());
                    userVO.setShield(friends.getShield());
                    userVO.setVisible(friends.getVisible());
                    list.add(userVO);
                }
            });
        }
        return list;
    }

    @Override
    public boolean isFriends(Long userIdA, Long userIdB, Integer type) {
        List<Friends> friendsList = friendsManager.listByUserIdAndUserFriendId(userIdA, userIdB, 1);

        if (CollectionUtils.isNotEmpty(friendsList) && friendsList.size() > 1) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void makeFriends(Long userIdA, Long UserIdB, Integer type) {
        ChatGroup friendsChatGroup = chatGroupManager.createFriendsChatGroup(userIdA, UserIdB);
        List<ChatGroupMember> list = Lists.newArrayList();
        ChatGroupMember chatGroupMember = new ChatGroupMember();
        chatGroupMember.setUserId(userIdA);
        chatGroupMember.setGroupType(friendsChatGroup.getGroupType());
        chatGroupMember.setChatGroupId(friendsChatGroup.getId());
        list.add(chatGroupMember);
        chatGroupMember = new ChatGroupMember();
        chatGroupMember.setUserId(UserIdB);
        chatGroupMember.setGroupType(friendsChatGroup.getGroupType());
        chatGroupMember.setChatGroupId(friendsChatGroup.getId());
        list.add(chatGroupMember);
        chatGroupMemberManager.batchSaveFriendMember(list, friendsChatGroup.getId());
        friendsManager.saveFriends(userIdA, UserIdB, type);
    }

    @Override
    public void shieldFriends(Long userId, Long friendsUserId) {
        Friends friends = friendsManager.getFriendsByUserIdAndFriendsId(userId, friendsUserId);
        friendsManager.updateShield(userId, friendsUserId, !friends.getShield());
    }

    @Override
    public void updateFriendsByUserIdAndFriendId(Long userId, Long friendId, Friends friends) {
        FriendsQuery query = new FriendsQuery();
        query.setUserId(userId);
        query.setFriendId(friendId);
        friendsManager.updateByQuery(friends, query);
    }

    @Override
    public Friends getFriendsByUserIdAndFriendId(Long userId, Long friendId) {
        return friendsManager.getFriendsByUserIdAndFriendsId(userId, friendId);
    }

    @Override
    public void deleteFriends(Long userId, Long friendsUserId) {
        friendsManager.deleteFriends(userId, friendsUserId);
    }
}
